<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>


<button id="button1">普通window.open跳转</button>
<br>

<button id="button2">创建A标签跳转</button>


<script>
  const windowOpenUrl = (href) => {
    const a = document.createElement('a');
    a.setAttribute('href', href);
    a.setAttribute('target', '_blank');
    a.setAttribute('id', 'windowOpenUrl');

    // 防止反复添加
    const element = document.getElementById('windowOpenUrl');
    if (element) {
      document.body.removeChild(element);
    }
    document.body.appendChild(a);
    a.click();
  };


  const button1 = document.getElementById('button1');
  const button2 = document.getElementById('button2');

  button1.onclick = () => {
    window.open('https://www.baidu.com');
  };

  button2.onclick = () => {
    windowOpenUrl('https://www.baidu.com')
  };
</script>

</body>
</html>
